﻿using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tateeda.ClinicalStudy.Base.Modules.Helpers.Utility;
using Tateeda.ClinicalStudy.Base.Modules.Models.Utility;

namespace CliniicalSudy.Base.Modules.Tests {

    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class CTGovHelperSerializationTest {
        #region sample xml

        private String xml = @"<?xml version='1.0' encoding='utf-8'?>
        <study_collection xmlns='http://clinicaltrials.gov/prs' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
        <clinical_study>
			<id_info>
				<provider_name/>
				<provider_study_id/>
				<org_name>CTGov-Username</org_name>
				<org_study_id>Test Protocal ID</org_study_id>
				<secondary_id>
					<id>Second ID</id>
					<id_type>US NIH Grant Number</id_type>
					<id_domain>Domain ID</id_domain>
				</secondary_id>
			</id_info>
            <is_fda_regulated>Yes</is_fda_regulated>
			<is_section_801>Yes</is_section_801>
			<delayed_posting>Yes</delayed_posting>
			<is_ind_study>Yes</is_ind_study>
			<ind_info >
				<ind_grantor>CBER</ind_grantor>
				<ind_number>23423423</ind_number>
				<ind_serial_number>werwer</ind_serial_number>
				<has_expanded_access>Yes</has_expanded_access>
				<expanded_access_nct_id>Expanded accdss record</expanded_access_nct_id>
			</ind_info>
            <brief_title >This is the title</brief_title>
			<acronym>this is the acronym</acronym>
			<official_title >oficial title </official_title>
			<sponsors >
				<lead_sponsor>
					<agency>sponsor</agency>
				</lead_sponsor>
				<collaborator>
					<agency>colaborators: name per line</agency>
				</collaborator>
				<resp_party>
					<name_title>Responsible party</name_title>
					<organization>organisation</organization>
					<phone>858-3530780</phone>
					<phone_ext/>
					<email>test@test.com</email>
				</resp_party>
			</sponsors>
			<oversight_info >
				<regulatory_authority>United States: Food and Drug Administration</regulatory_authority>
				<irb_info>
					<approval_status>Request not yet submitted</approval_status>
					<approval_number>1212121</approval_number>
					<name>Board Name * (required only if status is &quot;Submitted, approved&quot; or &quot;Submitted, exempt&quot;) </name>
					<affiliation>Board Affiliation *</affiliation>
					<phone>55555555555</phone>
					<phone_ext>ex</phone_ext>
					<email>email@tets.com</email>
					<full_address>business address here </full_address>
				</irb_info>
				<has_dmc>Yes</has_dmc>
			</oversight_info>
			<brief_summary >
				<textblock>Brief summary</textblock>
			</brief_summary>
			<detailed_description >
				<textblock>Detailed Description &#xd;Definition: Extended description of the protocol, including more technical information (as compared to the Brief Summary) if desired. Do not include the entire protocol; do not duplicate information recorded in other data elements, such as eligibility criteria or outcome measures. (Limit: 32,000 characters) &#xd;</textblock>
			</detailed_description>
			<overall_status >Recruiting</overall_status>
			<why_stopped />
			<expanded_access_status />
			<verification_date>2012-02</verification_date>
			<start_date >2012-01</start_date>
			<end_date/>
			<last_follow_up_date >2014-02</last_follow_up_date>
			<last_follow_up_date_type >Anticipated</last_follow_up_date_type>
			<primary_compl_date >2012-01</primary_compl_date>
			<primary_compl_date_type >Anticipated</primary_compl_date_type>
			<study_design>
				<study_type >Interventional</study_type>
				<interventional_design >
					<interventional_subtype>Treatment</interventional_subtype>
					<phase>Phase 0</phase>
					<assignment>Single Group Assignment</assignment>
					<allocation>Randomized Controlled Trial</allocation>
					<masking>Single Blind</masking>
					<masked_subject>true</masked_subject>
					<masked_caregiver>true</masked_caregiver>
					<masked_investigator>true</masked_investigator>
					<masked_assessor>true</masked_assessor>
					<control>Active</control>
					<endpoint>Efficacy</endpoint>
					<number_of_arms>3</number_of_arms>
				</interventional_design>
				<observational_design >
					<observational_study_design/>
					<biospecimen_retention/>
					<biospecimen_description>
						<textblock/>
					</biospecimen_description>
					<timing/>
					<number_of_groups/>
				</observational_design>
			</study_design>
            <primary_outcome >
				<outcome_measure>outcome measure title</outcome_measure>
				<outcome_time_frame>time frame</outcome_time_frame>
				<outcome_description>
					<textblock>description</textblock>
				</outcome_description>
				<outcome_safety_issue>Yes</outcome_safety_issue>
			</primary_outcome>
			<enrollment >100</enrollment>
			<enrollment_type >Anticipated</enrollment_type>
			<condition >Conditions or Focus of Study * FDAAA </condition>

			<keyword >Keywords</keyword>
<!--todo

            <keyword >Keywords11</keyword>
			<keyword >Keywords111</keyword>
-->
            <arm_group >
				<arm_group_label>arm label</arm_group_label>
				<arm_type>Sham Comparator</arm_type>
				<arm_group_description>
					<textblock>artm type is a drop down list</textblock>
				</arm_group_description>
			</arm_group>
			<intervention >
				<intervention_type>Drug</intervention_type>
				<intervention_name>Intervension name</intervention_name>
				<intervention_description>
					<textblock>description</textblock>
				</intervention_description>
				<intervention_other_name>other name 1</intervention_other_name>
				<intervention_other_name>Other name 2</intervention_other_name>
			</intervention>
			<eligibility >
				<study_population>
					<textblock/>
				</study_population>
				<sampling_method/>
				<criteria>
					<textblock>Eligibility Criteria * FDAAA </textblock>
				</criteria>
				<healthy_volunteers>Yes</healthy_volunteers>
				<gender>Both</gender>
				<minimum_age>18 Years</minimum_age>
				<maximum_age>99 Years</maximum_age>
			</eligibility>
			<overall_official >
				<first_name>study</first_name>
				<middle_name/>
				<last_name>offical</last_name>
				<degrees>doctor</degrees>
				<role>Study Director</role>
				<affiliation>org affiliation</affiliation>
			</overall_official>
			<overall_contact >
				<first_name>slava</first_name>
				<middle_name>x</middle_name>
				<last_name>khristich</last_name>
				<degrees>doctor</degrees>
				<phone>8583530780</phone>
				<phone_ext/>
				<email>x@y.com</email>
			</overall_contact>
			<overall_contact_backup >
				<first_name>firstname</first_name>
				<middle_name/>
				<last_name>last name</last_name>
				<degrees>degree</degrees>
				<phone>phone</phone>
				<phone_ext>ext</phone_ext>
				<email>email.@x.com</email>
			</overall_contact_backup>
			<location >
				<facility>
					<name>facility name</name>
					<address>
						<city>San Diego</city>
						<state>CA</state>
						<country>USA</country>
						<zip>92130</zip>
					</address>
				</facility>
				<status>Not yet recruiting</status>
				<contact>
					<first_name>Slava</first_name>
					<middle_name>X</middle_name>
					<last_name>Khristich</last_name>
					<degrees>Degree</degrees>
					<phone>8583530780</phone>
					<phone_ext>ex</phone_ext>
					<email>test@me.com</email>
				</contact>
				<contact_backup>
					<first_name>slava-1</first_name>
					<middle_name>y</middle_name>
					<last_name>khristich</last_name>
					<degrees>degree</degrees>
					<phone>8583530780</phone>
					<phone_ext>ex</phone_ext>
					<email>test@again.com</email>
				</contact_backup>
				<investigator>
					<first_name>investigator</first_name>
					<middle_name>mi</middle_name>
					<last_name>lastname</last_name>
					<degrees>doctor</degrees>
					<role>Principal Investigator</role>
				</investigator>
			</location>
			<link >
				<url>http://test.com</url>
				<description>description</description>
			</link>
    	  </clinical_study>
        </study_collection>";

        #endregion sample xml

        [TestMethod]
        public void CTGovHelperFromXmlTest() {
            CTGovSubmissionModel model = CTGovHelper.CTModelFromXml(xml, null);
            Assert.IsNotNull(model);
            Debug.WriteLine(model.SubmissionXML);
            //Debug.WriteLine("xml len " + model.SubmissionXML.Length);
            Assert.IsFalse(string.IsNullOrWhiteSpace(model.SubmissionXML));
            Assert.IsNotNull(model.StudyCollection);
            Assert.IsNotNull(model.StudyCollection.ClinicalSudy);
            Debug.WriteLine(model.StudyCollection.ClinicalSudy.IdInfo.OrgName);
            Assert.IsFalse(string.IsNullOrWhiteSpace(model.StudyCollection.ClinicalSudy.IdInfo.OrgName));
            Assert.IsNotNull(model.StudyCollection.ClinicalSudy.IdInfo);
            Assert.IsNotNull(model.StudyCollection.ClinicalSudy.IndInfo);
            Assert.IsNotNull(model.StudyCollection.ClinicalSudy.IndInfo.IndNumber > 0);
            var study = model.StudyCollection.ClinicalSudy;
            Assert.IsNotNull(study.BriefSummary);
            Assert.IsNotNull(study.BriefSummary.TextBlock);
            Assert.IsFalse(string.IsNullOrWhiteSpace(study.BriefSummary.TextBlock));

            Debug.WriteLine(study.PrimaryComplDate);
            Assert.IsNotNull(study.StudyDesign);

            Assert.IsNotNull(study.PrimaryOutcome);

            Assert.IsNotNull(study.ArmGroup);

            Assert.IsNotNull(study.Intervention);
            Assert.IsNotNull(study.Location);
            Assert.IsNotNull(study.Link);

            // convert back to xml
            var xml2 = CTGovHelper.CTModelToXml(model);
            Debug.WriteLine(xml2);
            //Debug.WriteLine("round-trip xml len " + xml2.Length);
            Assert.IsFalse(string.IsNullOrWhiteSpace(xml2));
        }

        [TestMethod]
        public void CTGovHelperToXmlTest() {
            var model = MakeTestModel2();
            var xml2 = CTGovHelper.CTModelToXml(model);
            Debug.WriteLine(xml2);
            Assert.IsFalse(string.IsNullOrWhiteSpace(xml2));
        }

        private CTGovSubmissionModel MakeTestModel() {
            CTGovSubmissionModel result = new CTGovSubmissionModel();
            // create test object from our sample xml
            result.SubmissionXML = xml;
            StudyCollection coll;
            SerializationHelper.FromXml(xml, out coll);
            result.StudyCollection = coll;
            return result;
        }

        private CTGovSubmissionModel MakeTestModel2() {
            CTGovSubmissionModel result = new CTGovSubmissionModel {
                StudyCollection = new StudyCollection {
                    ClinicalSudy = new ClinicalSudy {
                        IdInfo = new IdInfo {
                            OrgName = "Org",
                            OrgStudyId = "1001",
                            ProviderName = "Provider",
                            ProviderStudyId = "2002"
                        }
                    }
                }
            };
            return result;
        }
    }
}